﻿/***
 * 
 *    Title: "GodGame" 项目的简易UI框架
 *           主题： 总结算中的头像     
 *    Description: 
 *           功能： 总结算界面的 头像 及FTG EXP SOUL等信息
 *
 *    Date: 2020
 *    Version: 0.1版本
 *    Modify Recoder: 
 *    
 *   
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody2D rb;
    private BoxCollider2D coll;

    [Header("移动参数")]
    public float speed = 8f;
    public float crouchSpeedDivisor = 3f;
    [Header("跳跃参数")]
    public float jumpForce = 6.3F;
    public float jumpHoldForce = 1.9f;
    public float jumpHoldDuration = 0.1f;
    public float crouchJumpBoost = 2.5f;

    float jumpTime;

    [Header("状态")]
    public bool isCrouch;
    public bool isOnGround;
    public bool isJump;

    [Header("环境监测")]
    public LayerMask groundLayer;

    float xVelocity;


    // 按键设置
    bool jumpPressed;
    bool jumpHeld;
    bool crouchHeld;


    // 碰撞体尺寸
    Vector2 colliderStandSize;
    Vector2 colliderStandOffset;
    Vector2 colliderCrouchSize;
    Vector2 colliderCrouchOffset;


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();

        colliderStandSize = coll.size;
        colliderStandOffset = coll.offset;

        colliderCrouchSize = new Vector2(colliderStandSize.x ,colliderStandSize.y / 2F);
        colliderCrouchOffset = new Vector2(colliderStandOffset.x, colliderStandOffset.y / 2F);
    }

    private void Update()
    {
        jumpPressed = Input.GetButtonDown("Jump");
        jumpHeld = Input.GetButton("Jump");
        crouchHeld = Input.GetButton("Crouch");
    }

    private void FixedUpdate()
    {
        PhysicsCheck();
        GroundMovement();
    }

    void PhysicsCheck() {
        if (coll.IsTouchingLayers(groundLayer))
        {
            isOnGround = true;
        }
        else {
            isOnGround = false;
        }
    }

    void GroundMovement() {

        if (crouchHeld) {
            Crouch();
        } else if (!crouchHeld && isCrouch) {
            StandUp();
        }

        xVelocity = Input.GetAxis("Horizontal");

        if (isCrouch) {
            xVelocity /= crouchSpeedDivisor;
        }

        rb.velocity = new Vector2(xVelocity * speed ,rb.velocity.y);

        FilpDirection();
    }




    void FilpDirection() {
        if (xVelocity < 0) {
            transform.localScale = new Vector3(-1,1,1);
        }
        if (xVelocity > 0)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
    }

    void Crouch() {
        isCrouch = true;

        coll.size = colliderCrouchSize;
        coll.offset = colliderCrouchOffset;
    }

    void StandUp() {
        isCrouch = false;
        coll.size = colliderStandSize;
        coll.offset = colliderStandOffset;
    }



}//--ClassEnd
